package l1j.opqlo.NewSystem.Extension.FireRoom;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.Instance.L1EffectInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.collections.Maps;

public class FireRoomThread implements Runnable {
	private static Logger _log = Logger.getLogger(FireRoomThread.class.getName());
	private static FireRoomThread _instance;

	private static Map<Integer , FireRoom>ROOM_LIST;
	private static int[] ROOM_MAPS_ID = { 9902, 9903, 9904, 9905, 9906, 9907,9908, 9909, 9910, 9911 };
	private static Set<Integer>USE_MAP;
	private static Map<Integer , FireRoom>ROOM_MAP;


	public static FireRoomThread get(){
		if(_instance == null){
			_instance = new FireRoomThread();
		}
		return _instance;
	}

	public FireRoomThread(){
		ROOM_LIST = Maps.newConcurrentMap();
		USE_MAP = new HashSet<Integer>();
		ROOM_MAP = Maps.newConcurrentMap();
		GeneralThreadPool.getInstance().execute(this);
	}

	/**踏板觸發任務**/
	public void actionTrap(final L1EffectInstance effect){
		final int mapid = effect.getMapId();
		final FireRoom room = ROOM_LIST.get(mapid);
		if(room !=null){
			room.TrapAcvion(effect);
		}
	}

	/**新增副本*/
	public boolean CreateRoom(final L1PcInstance pc){
		if(USE_MAP.size() <10){
			if(!ROOM_MAP.isEmpty()){
				ROOM_MAP.remove(pc.getId());
			}
			boolean canJoin = false;
			for(final int mapid : ROOM_MAPS_ID){
				if(USE_MAP.add(mapid)){
					canJoin = true;
					final FireRoom room = new FireRoom((short) mapid , pc.getParty());
					ROOM_LIST.remove(mapid);
					ROOM_LIST.put(mapid, room);
					ROOM_MAP.put(pc.getId(), room);
					break;
				}
			}
			if(!canJoin){
				pc.sendPackets(new S_SystemMessage("目前副本地圖已經全部額滿，請稍後再嘗試。"));
				return true;
			}
		}else{
			pc.sendPackets(new S_SystemMessage("目前副本地圖已經全部額滿，請稍後再嘗試。"));
			return false;
		}
		return true;
	}

	/**重新加入副本**/
	public void reJoinRoom(final L1PcInstance pc){
		FireRoom room = ROOM_MAP.get(pc.getId());
		if(room !=null){
			room.reJoinRoom(pc);
		}else{
			pc.sendPackets(new S_SystemMessage("你沒有任何可進入的副本。"));	
		}
	}

	/**使用魔族密令召喚BOSS**/
	public void spawnBoss(final int mapid){
		final FireRoom room = ROOM_LIST.get(mapid);
		if(room !=null){
			room.setStatus(3);
		}
	}

	public void bossDead(final int mapid , final int npcid){
		final FireRoom room = ROOM_LIST.get(mapid);
		if(room !=null){
			room.npdDead(npcid);
		}
	}


	@Override
	public void run(){
		while(true){
			if(!ROOM_LIST.isEmpty()){
				try{
					final Collection<FireRoom>list = ROOM_LIST.values();
					final Iterator<FireRoom> ite =list.iterator();
					while(ite.hasNext()){
						final FireRoom room = ite.next();
						if(!room.RunGame()){
							ROOM_LIST.remove(room);
							USE_MAP.remove(room.getMapId());
							final Collection<FireRoom> roomlist =ROOM_MAP.values();
							final Iterator<FireRoom>roomite = roomlist.iterator();
							while(roomite.hasNext()){
								final FireRoom roomtemp = roomite.next();
								if(roomtemp.getMapId() == room.getMapId()){
//									roomite.remove();
									ROOM_MAP.remove(roomtemp);
								}
							}
						}
					}
				}catch(Exception e){
					_log.log(Level.SEVERE, e.getLocalizedMessage() , e);
					e.printStackTrace();
				}
			}
			try {
				Thread.sleep(1001);
			} catch (InterruptedException e) {
			}
		}
	}

}
